<?php


namespace Modules\Cms\Http\Controllers\Web;


use App\Helpers\ViewHelpers;
use App\Http\Controllers\MyController;
use Modules\Cms\Http\Requests\ArticleCommentRequest;
use Modules\Cms\Models\Article;
use Modules\Cms\Models\ArticleCategory;
use Modules\Cms\Models\ArticleComment;
use Modules\Cms\Models\ArticleTag;
use Addons\Ads\Models\Ads;
use Modules\User\Models\UserPoint;
use Illuminate\Http\Request;
use App\Models\UserInfo;
use Illuminate\Support\Facades\DB;
use Addons\School\Models\School;
use Illuminate\Support\Facades\Auth;
class TopController extends MyController
{

    use ViewHelpers;

    public function persion(Request $request)
    {
        $id=$request->get('id');
        session([
            'the_page' => 'top',
            'page_title' => "活动排名-个人",
        ]);
        $uid=Auth::check()?auth()->user()->id:0;
        //1 获取前 3
        $list=UserPoint::select('user_id','point',DB::raw('SUM(point) as rose'))->with('user')->where('aid',$id)->groupBy('user_id')->orderBy('rose','desc')->get();
 
        $l=[];
        $all=[];
        $one=null;
        $two=null;
        $three=null;
        foreach ($list as $k=>$v){
            $v->x=$k+1;
            if($k<3){
                 if($k==0){
                     $one=$v;
                 }
                 if($k==1){
                     $two=$v;
                 }
                 if($k==2){
                     $three=$v;
                 }
            }else{
                $l[$v->user_id]=$v;
            }
            $all[$v->user_id]=$v;
     
        }
        $my=!empty($all[$uid])?$all[$uid]:'';
 
       
        return $this->theme('toppersion',['one'=>$one,'two'=>$two,'three'=>$three,'list'=>$l,'my'=>$my,'id'=>$id]);
    }
   
    public function school(Request $request)
    {
       
        $id=$request->get('id');
         session([
            'the_page' => 'top',
            'page_title' => "活动排名-学校",
        ]);
        $list=UserPoint::select('user_id','point',DB::raw('SUM(point) as rose'))->with('info')->where('aid',$id)->groupBy('user_id')->orderBy('rose','desc')->get();
        $all=[];
        foreach ($list as $k=>$v){
            $x=School::where('name',$v->info->school_name)->first();
            $all[$v->info->school_name]['info']=$x;
            if(empty($all[$v->info->school_name]['rose'])){
               $all[$v->info->school_name]['rose']=$v->rose;
            }else{
                $all[$v->info->school_name]['rose']=$all[$v->info->school_name]['rose']+$v->rose;
            }
        }
        array_multisort(array_column($all, 'rose'),SORT_DESC,$all);
        $i=0;
        $one=null;
        $two=null;
        $three=null;
        $l=[];
        foreach ($all as $v){
            $v['x']=$i+1;
             if($i<3){
                 if($i==0){
                     $one=$v;
                 }
                 if($i==1){
                     $two=$v;
                 }
                 if($i==2){
                     $three=$v;
                 }
            }else{
               $l[]=$v;
            }
            $i++;
        }
        return $this->theme('topschool',['one'=>$one,'two'=>$two,'three'=>$three,'list'=>$l,'id'=>$id]);
    }
   
}
